let currentDir: 'ltr' | 'rtl' | 'auto' = 'auto'

// let currentDir = "auto";

function hasDocument () {
  return (typeof document !== 'undefined')
}

function hasWindow () {
  return (typeof window !== 'undefined')
}

export function getDocumentDir () {
  if (!hasDocument()) {
    return currentDir
  }
  return (typeof document.dir !== 'undefined')
    ? document.dir
    : document.getElementsByTagName('html')[0].getAttribute('dir')
}

export function setDocumentDir (dir: 'ltr' | 'rtl' | 'auto') {
  if (!hasDocument) {
    currentDir = dir
    return
  }
  const html = document.getElementsByTagName('html')[0]
  html.setAttribute('dir', dir)
}

export function addWindowEventListener (event: string, callback: () => void) {
  if (!hasWindow) {
    callback()
    return
  }
  window.addEventListener(event, callback)
}

export function removeWindowEventListener (event: string, callback: () => void) {
  if (!hasWindow) {
    return
  }
  window.removeEventListener(event, callback)
}
